﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class NhapHang : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmNhapHang";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 10;
    static int CurrentPage = 1;
    static double kltt = 0, klmua = 0, klquidoi=0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            GetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {
                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    Title = "Quản lý nhập hàng | " + Header;
                    klquidoi = kltt = klmua = 0;
                    btnCancel_Click(sender, e);
                    for (int i = 1; i <= 12; i++)
                    {
                        dlThang.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
                    }
                    dlThang.Items.Insert(0, new ListItem("Theo thời gian", ""));
                    dlThang.SelectedIndex = 0;

                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year2; i >= year1; i--)
                    {
                        dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                    }
                    dlNam.SelectedValue = year2.ToString();

                    dlLoaiVLLoc.Items.Clear();
                    dlLoaiVLLoc.Items.Insert(0, new ListItem("--Chọn loại vật liệu--", ""));

                    dlBenChayCuocLoc.Items.Clear();
                    dlBenChayCuocLoc.Items.Insert(0, new ListItem("--Chọn bên chạy cước--", ""));

                    p = new SqlParameter[2];
                    p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(txtTuNgay.Text.Trim()));
                    p[0].SqlDbType = SqlDbType.SmallDateTime;
                    p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(txtDenNgay.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.SmallDateTime;

                    dlNhaCungCapLoc.DataSource = ex.GetData("sp_xdanhaphang_ListBenBanByTime", p);
                    dlNhaCungCapLoc.DataBind();
                    dlNhaCungCapLoc.Items.Insert(0, new ListItem("--Chọn bên bán--", ""));
                }
                else
                    GetMess("Bạn không có quyền xem trang này", "Home.aspx");
            }
        }
    }
    void LoadLoaiVatLieu(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaloaivatlieu_LoadDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn vật liệu--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadNhaCungCap(DropDownList dl, string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdanhacungcap_LoadDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }
    void LoadLaiXe(DropDownList dlLaiXe)
    {
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@NgayNhap", ex.GetNgayThang(txtNgayNhap.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;

        dlLaiXe.Items.Clear();
        dlLaiXe.DataSource = ex.GetData("sp_xdanhaphang_ListLaiXe",p);
        dlLaiXe.DataBind();
        dlLaiXe.Items.Insert(0, new ListItem("--Chọn lái xe--", ""));
        dlLaiXe.SelectedIndex = 0;
    }
    void LoadBienSoXe(DropDownList dlBienSo)
    {
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@ThoiGian", ex.GetNgayThang(txtNgayNhap.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;

        dlBienSo.Items.Clear();
        dlBienSo.DataSource = ex.GetData("sp_xdanhaphang_GetBienSo", p);
        dlBienSo.DataBind();
        dlBienSo.Items.Insert(0, new ListItem("--Chọn biển số xe--", ""));
        dlBienSo.SelectedIndex = 0;
    }
    void LoadBienSoXeAll(DropDownList dlBienSo)
    {
        
        dlBienSo.Items.Clear();
        dlBienSo.DataSource = ex.GetData("sp_xdadoituongchi_ListAll");
        dlBienSo.DataBind();
        dlBienSo.Items.Insert(0, new ListItem("--Chọn biển số xe--", ""));
        dlBienSo.SelectedIndex = 0;
    }
    void LoadTramTron(DropDownList dl)
    {
        dlTramTron.Items.Clear();
        dlTramTron.DataSource = ex.GetData("sp_xdanhaphang_ListTramTron");
        dlTramTron.DataBind();
        dlTramTron.Items.Insert(0, new ListItem("--Chọn trạm trộn--", ""));
        dlTramTron.SelectedIndex = 0;
    }
    void LoadTramTronAll(DropDownList dl)
    {
        dlTramTron.Items.Clear();
        dlTramTron.DataSource = ex.GetData("sp_xdatbltramtron_ListAll");
        dlTramTron.DataBind();
        dlTramTron.Items.Insert(0, new ListItem("--Chọn trạm trộn--", ""));
        dlTramTron.SelectedIndex = 0;
    }
    void LoadLoaiVatLieuAll(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaloaivatlieu_LoadAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn loại vật liệu--",""));
        dl.SelectedIndex = 0;
    }
    void LoadNhaCungCapAll(DropDownList dl, string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdanhacungcap_LoadAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }
    void LoadLaiXeAll(DropDownList dlLaiXe)
    {
        dlLaiXe.Items.Clear();
        dlLaiXe.DataSource = ex.GetData("sp_xdaxevanchuyen_ListLaiXeAll");
        dlLaiXe.DataBind();
        dlLaiXe.Items.Insert(0, new ListItem("--Chọn lái xe--", ""));
        dlLaiXe.SelectedIndex = 0;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtBienSo.Visible.Equals(true))
            {
                if (txtBienSo.Text.Trim().Equals(""))
                    GetMess("Nhập biển số", "");
                else
                {
                    InsertPhieuNhap(1, Guid.NewGuid());
                    UpdateAutoNumber();
                    btnCancel_Click(sender, e);
                    GetMess("Lưu thành công", "");
                }
            }
            else
            {
                if (dlBienSo.SelectedIndex.Equals(0))
                    GetMess("Chọn biển số", "");
                else
                {
                    InsertPhieuNhap(1, Guid.NewGuid());
                    UpdateAutoNumber();
                    btnCancel_Click(sender, e);
                    GetMess("Lưu thành công", "");
                }
            }
        }
        catch (Exception ax)
        {
            GetMess(ax.Message, "");
        }
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        if (f_Sua.Equals(1))
        {
            Guid ids = new Guid(hdID.Value);
            InsertPhieuNhap(2, ids);
            btnCancel_Click(sender, e);
            GetMess("Cập nhật thành công", "");
        }
        else
            GetMess("Bạn không có quyền sửa trên trang này", "");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtNgayNhap.Text = DateTime.Now.ToString("dd/MM/yyyy");

        txtSoPhieu.Text = LoadSoPhieu();

        txtGioNhap.Text = DateTime.Now.Hour.ToString() + "h" + DateTime.Now.Minute.ToString();
        
        txtTon.Text = "";
        txtBienSo.Text = "";
        txtLaiXe.Text = "";
        
        txtTLTong.Text = "";
        txtTLBi.Text = "";
        txtTLHang.Text = "";
        txtKLMua.Text = "";
        txtKLThucTe.Text = "";
        txtDoAm.Text = "";
        txtTiLeQuiDoi.Text = "";
        txtKLSauQuiDoi.Text = "";
        txtDVT.Text = "";
        txtGhiChu.Text = "";

        txtTuNgay.Visible = true;
        txtDenNgay.Visible = true;
        txtTuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
        txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
        
        hdLaiXeID.Value = "";
        btncapnhat.Visible = false;
        btnSave.Visible = true;
        btnFirst.Visible = true;
        btnPre.Visible = true;
        btnNext.Visible = true;

        LoadLoaiVatLieu(dlLoaiVatLieu);
        LoadNhaCungCap(dlBenChayCuoc, "--Chọn bên chạy cước--");
        LoadNhaCungCap(dlBenBan, "--Chọn bên bán--");
        LoadLaiXe(dlLaiXe);
        LoadTramTron(dlTramTron);
        LoadBienSoXe(dlBienSo);

        ckXeNha.Checked = false;
        ckQuaCan.Checked = true;
        ckCuocVCVAT.Checked = false;
        ckKLMuaVAT.Checked = false;
        ckQuaCan_CheckedChanged(sender, e);

        dlTramTron.SelectedIndex = 0;
        dlLoaiVatLieu.SelectedIndex = 0;
        dlBenChayCuoc.SelectedIndex = 0;

        dlBienSo.Visible = false;
        txtBienSo.Visible = true;

        txtLaiXe.Enabled = true;
        dlBienSo.Enabled = true;
        dlLaiXe.Enabled = true;
        dlTramTron.Enabled = true;
        dlLoaiVatLieu.Enabled = true;
        dlBenChayCuoc.Enabled = true;
        dlBenBan.Enabled = true;
        dlNam.Visible = false;
        dlThang.SelectedIndex = 0;

        dlTramTron.SelectedValue = Session["IDTramTron"].ToString();
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdanhaphang_List", p);
        GV.DataBind();
        txtNgayNhap.Enabled = true;
        hdID.Value = "";
        btnSave.Visible = CheckKhoa2(txtNgayNhap.Text.Trim(), frmName, Session["user"].ToString());

        
    }
    private bool CheckKhoa(string ngaythang)
    {
        //load trang thai khoa
        if (!ngaythang.Trim().Equals(""))
        {

            p = new SqlParameter[3];
            p[0] = new SqlParameter("@Name", "BCNH");
            p[0].SqlDbType = SqlDbType.NVarChar;
            p[1] = new SqlParameter("@Thang", int.Parse(ngaythang.Trim().Split('/')[1]));
            p[1].SqlDbType = SqlDbType.Int;
            p[2] = new SqlParameter("@Nam", int.Parse(ngaythang.Trim().Split('/')[2]));
            p[2].SqlDbType = SqlDbType.Int;

            string index = ex.GetValueString("sp_xdakhoa_Check", p);

            if (index.Trim().Equals("") || index.Trim().Equals("1"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else return false;
    }
    private bool CheckKhoa2(string ngaythang,string name, string nhanvien)
    {
        //load trang thai khoa
        if (!ngaythang.Trim().Equals(""))
        {

            p = new SqlParameter[3];
            p[0] = new SqlParameter("@Name", name);
            p[0].SqlDbType = SqlDbType.NVarChar;
            p[1] = new SqlParameter("@Ngay", ex.GetNgayThang(ngaythang));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@NhanVien", nhanvien);
            p[2].SqlDbType = SqlDbType.NVarChar;

            string index = ex.GetValueString("sp_KhoaMo_Check", p);

            if (index.Trim().Equals("2"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else return false;
    }
    private string LoadSoPhieu()
    {
        string sophieu = "0";
        try
        {
            if (dlLoaiVatLieu.SelectedIndex > 0)
            {
                p = new SqlParameter[2];
                p[0] = new SqlParameter("@ThoiGian", ex.GetNgayThang(txtNgayNhap.Text.Trim()));
                p[0].SqlDbType = SqlDbType.SmallDateTime;
                p[1] = new SqlParameter("@LoaiVL", ex.GetGuid(dlLoaiVatLieu.SelectedValue));
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                sophieu = ex.GetValueString("sp_GetSoPhieuNhap", p).Trim();
                txtSoPhieu.Text = sophieu;
            }
        }
        catch
        {
            
        }
        return sophieu;
    }

    protected void dlBenChayCuoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ckXeNha.Checked == true && dlBenChayCuoc.SelectedItem.Text.Trim().ToUpper().Contains("ĐỨC ANH"))
        {
            dlLaiXe.Visible = false;
            txtLaiXe.Visible = true;

            dlBienSo.Visible = true;
            txtBienSo.Visible = false;

            txtBienSo.Text = "";
            dlLaiXe.SelectedIndex = 0;
            txtLaiXe.Text = "";
        }
        else
        {
            //txtBienSo.Text = "";
            dlLaiXe.Visible = false;
            txtLaiXe.Visible = true;

            dlBienSo.Visible = false;
            txtBienSo.Visible = true;
            txtLaiXe.Text = "";
            txtBienSo.Enabled = true;
        }
    }
    
    protected void ckQuaCan_CheckedChanged(object sender, EventArgs e)
    {
        if (ckQuaCan.Checked)
        {
            txtTLTong.Enabled = true;
            txtTLBi.Enabled = true;
            txtTLHang.Enabled = true;
            txtKLThucTe.Enabled = false;
            txtKLThucTe.Text = "";
        }
        else
        {
            txtTLTong.Enabled = false;
            txtTLBi.Enabled = false;
            txtTLHang.Enabled = false;
            txtKLThucTe.Enabled = true;

            txtTLTong.Text = "";
            txtTLBi.Text = "";
            txtTLHang.Text = "";
        }
    }
    protected void dlLaiXe_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@LaiXe", ex.GetGuid(dlLaiXe.SelectedValue));
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            p[1] = new SqlParameter("@ThoiGian", ex.GetNgayThang(txtNgayNhap.Text.Trim()));
            p[1].SqlDbType = SqlDbType.SmallDateTime;

            txtBienSo.Text = ex.GetValueString("sp_xdanhaphang_GetBienSoByLaiXe", p);
        }
        catch
        {
            txtBienSo.Text = "";
        }
    }
    protected void txtTLTong_TextChanged(object sender, EventArgs e)
    {
        string tong = txtTLTong.Text.Trim().Replace(",", "");
        if (!tong.Equals(""))
        {
            if (double.Parse(tong) > 999)
                txtTLTong.Text = string.Format("{0:0,0}", double.Parse(tong));
            else txtTLTong.Text = tong;
        }
        else txtTLTong.Text = "0";

        double tlTong = txtTLTong.Text.Trim().Replace(",", "").Equals("") ? 0 : double.Parse(txtTLTong.Text.Trim().Replace(",", ""));
        double tlBi = txtTLBi.Text.Trim().Replace(",", "").Equals("") ? 0 : double.Parse(txtTLBi.Text.Trim().Replace(",", ""));
        txtTLHang.Text = (tlTong - tlBi) > 999 ? string.Format("{0:0,0}", tlTong - tlBi) : (tlTong - tlBi).ToString();
        txtDoAm_TextChanged(sender, e);
        txtTLBi.Focus();
    }
    protected void txtTLBi_TextChanged(object sender, EventArgs e)
    {
        string tong = txtTLBi.Text.Trim().Replace(",", "");
        if (!tong.Equals(""))
        {
            if (double.Parse(tong) > 999)
                txtTLBi.Text = string.Format("{0:0,0}", double.Parse(tong));
            else txtTLBi.Text = tong;
        }
        else txtTLBi.Text = "0";
        double tlTong = txtTLTong.Text.Trim().Replace(",", "").Equals("") ? 0 : double.Parse(txtTLTong.Text.Trim().Replace(",", ""));
        double tlBi = txtTLBi.Text.Trim().Replace(",", "").Equals("") ? 0 : double.Parse(txtTLBi.Text.Trim().Replace(",", ""));
        txtTLHang.Text = (tlTong - tlBi) > 999 ? string.Format("{0:0,0}", tlTong - tlBi) : (tlTong - tlBi).ToString();
        txtDoAm_TextChanged(sender, e);
        txtKLMua.Focus();
    }
    protected void txtDoAm_TextChanged(object sender, EventArgs e)
    {
        if (ckQuaCan.Checked)
        {
            double tlhang = double.Parse(txtTLHang.Text.Trim().Replace(",", "").Equals("") ? "0" : txtTLHang.Text.Trim().Replace(",", ""));
            double doam = double.Parse(txtDoAm.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDoAm.Text.Trim().Replace(",", ""));
            txtKLSauQuiDoi.Text = Math.Round((tlhang - (tlhang * doam) / 100), 0).ToString();
            double klqd = double.Parse(txtKLSauQuiDoi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLSauQuiDoi.Text.Trim().Replace(",", ""));
            double tile = double.Parse(txtTiLeQuiDoi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtTiLeQuiDoi.Text.Trim().Replace(",", ""));

            txtKLThucTe.Text = Math.Round((klqd / tile), 1).ToString();

        }
        else
        {
            double kltt = double.Parse(txtKLThucTe.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLThucTe.Text.Trim().Replace(",", ""));
            double tile = double.Parse(txtTiLeQuiDoi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtTiLeQuiDoi.Text.Trim().Replace(",", ""));
            double doam = double.Parse(txtDoAm.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDoAm.Text.Trim().Replace(",", ""));
            txtKLSauQuiDoi.Text = Math.Round(((kltt * tile) - (kltt * tile * doam) / 100), 0).ToString();
        }
    }
    protected void txtTiLeQuiDoi_TextChanged(object sender, EventArgs e)
    {
        if (ckQuaCan.Checked == false)
        {
            double kltt = double.Parse(txtKLThucTe.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLThucTe.Text.Trim().Replace(",", ""));
            double tile = double.Parse(txtTiLeQuiDoi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtTiLeQuiDoi.Text.Trim().Replace(",", ""));
            double doam = double.Parse(txtDoAm.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDoAm.Text.Trim().Replace(",", ""));
            txtKLSauQuiDoi.Text = Math.Round(((kltt * tile) - (kltt * tile * doam) / 100), 0).ToString();
        }
        else
        {
            double tlqd = double.Parse(txtKLSauQuiDoi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLSauQuiDoi.Text.Trim().Replace(",", ""));
            double tile = double.Parse(txtTiLeQuiDoi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtTiLeQuiDoi.Text.Trim().Replace(",", ""));
            txtKLThucTe.Text = Math.Round((tlqd / tile), 1).ToString();
        }
    }
    void InsertPhieuNhap(int index, Guid ID)
    {
        try
        {
            SqlParameter[] p;
            p = new SqlParameter[28];
            if (index == 1)
            {
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            }
            else
            {
                p[0] = new SqlParameter("@IDs", ID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            }
            p[1] = new SqlParameter("@SoPhieu", LoadSoPhieu());
            p[1].SqlDbType = SqlDbType.NVarChar;

            p[2] = new SqlParameter("@NgayNhap", ex.GetNgayThang(txtNgayNhap.Text.Trim()));
            p[2].SqlDbType = SqlDbType.SmallDateTime;
            
            p[3] = new SqlParameter("@GioNhap", txtGioNhap.Text.Trim());
            p[3].SqlDbType = SqlDbType.NVarChar;
            p[4] = new SqlParameter("@TramTronID", ex.GetGuid(dlTramTron.SelectedValue));
            p[4].SqlDbType = SqlDbType.UniqueIdentifier;
            p[5] = new SqlParameter("@BienSo", txtBienSo.Text.Trim());
            p[5].SqlDbType = SqlDbType.NVarChar;

            object LX = DBNull.Value;
            try
            {
                if(dlBienSo.Visible.Equals(true))
                    LX = new Guid(hdLaiXeID.Value);
            }
            catch
            { 
                
            }
            p[6] = new SqlParameter("@LaiXe", LX);
            p[6].SqlDbType = SqlDbType.UniqueIdentifier;
            p[7] = new SqlParameter("@TenLaiXe", txtLaiXe.Text.Trim());
            p[7].SqlDbType = SqlDbType.NVarChar;
            p[8] = new SqlParameter("@LoaiVL", ex.GetGuid(dlLoaiVatLieu.SelectedValue));
            p[8].SqlDbType = SqlDbType.UniqueIdentifier;
            p[9] = new SqlParameter("@BenBan", ex.GetGuid(dlBenBan.SelectedValue));
            p[9].SqlDbType = SqlDbType.UniqueIdentifier;
            p[10] = new SqlParameter("@BenChayCuoc", ex.GetGuid(dlBenChayCuoc.SelectedValue));
            p[10].SqlDbType = SqlDbType.UniqueIdentifier;
            p[11] = new SqlParameter("@TLTong", double.Parse(txtTLTong.Text.Trim().Replace(",", "").Equals("") ? "0" : txtTLTong.Text.Trim().Replace(",", "")));
            p[11].SqlDbType = SqlDbType.Float;
            p[12] = new SqlParameter("@TLBi", double.Parse(txtTLBi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtTLBi.Text.Trim().Replace(",", "")));
            p[12].SqlDbType = SqlDbType.Float;
            p[13] = new SqlParameter("@TLHang", double.Parse(txtTLHang.Text.Trim().Replace(",", "").Equals("") ? "0" : txtTLHang.Text.Trim().Replace(",", "")));
            p[13].SqlDbType = SqlDbType.Float;
            p[14] = new SqlParameter("@KLMua", double.Parse(txtKLMua.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLMua.Text.Trim().Replace(",", "")));
            p[14].SqlDbType = SqlDbType.Float;
            p[15] = new SqlParameter("@KLThucTe", double.Parse(txtKLThucTe.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLThucTe.Text.Trim().Replace(",", "")));
            p[15].SqlDbType = SqlDbType.Float;
            p[16] = new SqlParameter("@DoAm", double.Parse(txtDoAm.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDoAm.Text.Trim().Replace(",", "")));
            p[16].SqlDbType = SqlDbType.Float;
            p[17] = new SqlParameter("@TiLeQuiDoi", double.Parse(txtTiLeQuiDoi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtTiLeQuiDoi.Text.Trim().Replace(",", "")));
            p[17].SqlDbType = SqlDbType.Float;
            p[18] = new SqlParameter("@KLSauQuiDoi", double.Parse(txtKLSauQuiDoi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLSauQuiDoi.Text.Trim().Replace(",", "")));
            p[18].SqlDbType = SqlDbType.Float;
            p[19] = new SqlParameter("@DonViTinh", txtDVT.Text.Trim());
            p[19].SqlDbType = SqlDbType.NVarChar;
            p[20] = new SqlParameter("@GhiChu", txtGhiChu.Text.Trim());
            p[20].SqlDbType = SqlDbType.NVarChar;
            p[21] = new SqlParameter("@QuaCan", ckQuaCan.Checked ? 1 : 0);
            p[21].SqlDbType = SqlDbType.Int;
            p[22] = new SqlParameter("@XeNha", ckXeNha.Checked ? 1 : 0);
            p[22].SqlDbType = SqlDbType.Int;
            p[23] = new SqlParameter("@NguoiTao", Session["user"].ToString());
            p[23].SqlDbType = SqlDbType.NVarChar;
            p[24] = new SqlParameter("@NgayTao", DateTime.Now);
            p[24].SqlDbType = SqlDbType.SmallDateTime;
            p[25] = new SqlParameter("@KLMuaVAT", ckKLMuaVAT.Checked?1:2);
            p[25].SqlDbType = SqlDbType.Int;
            p[26] = new SqlParameter("@CuocVCVAT", ckCuocVCVAT.Checked?1:2);
            p[26].SqlDbType = SqlDbType.Int;
            p[27] = new SqlParameter("@BienSoID", ex.GetGuid(dlBienSo.SelectedValue));
            p[27].SqlDbType = SqlDbType.UniqueIdentifier;

            if (index == 1)
            {
                ex.ExecuteNonQuerry("sp_xdanhaphang_Add", p);
            }
            else ex.ExecuteNonQuerry("sp_xdanhaphang_Update", p);
        }
        catch (Exception ax)
        {
            GetMess("Có lỗi trong quá trình cập nhật, xem trong file log", "");
            ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tNhập hàng\t" + ax.Message);
        }
    }
    private void GetMess(string Mess, string Link)
    {
        if (Link == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + Mess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + Mess + "');window.location.href='" + Link + "'", true);

    }

    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        SqlParameter[] p;
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            DataTable dt = ex.GetData("sp_xdanhaphang_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                txtNgayNhap.Text = dt.Rows[0]["NgayNhapHang"].ToString();

                txtNgayNhap_TextChanged(sender, e);
                //LoadLoaiVatLieuAll(dlLoaiVatLieu);
                //LoadNhaCungCapAll(dlBenChayCuoc, "--Chọn bên chạy cước--");
                //LoadNhaCungCapAll(dlBenBan, "--Chọn bên mua--");
                LoadLaiXeAll(dlLaiXe);
                //LoadTramTronAll(dlTramTron);
                //LoadBienSoXeAll(dlBienSo);
                
                ckXeNha.Checked = dt.Rows[0]["XeNha"].ToString().Trim().Equals("1") ? true : false;
                ckQuaCan.Checked = dt.Rows[0]["QuaCan"].ToString().Trim().Equals("1") ? true : false;
                dlBenChayCuoc.SelectedValue = dt.Rows[0]["BenChayCuoc"].ToString();

                ckQuaCan_CheckedChanged(sender, e);
                dlBenChayCuoc_SelectedIndexChanged(sender, e);

                //if (dlBienSo.Visible.Equals(true))
                //{
                //    dlBienSo.Enabled = false;
                //    txtLaiXe.Enabled = false;
                //}
                //else
                //{
                //    txtLaiXe.Enabled = true;
                //}
                txtSoPhieu.Text = dt.Rows[0]["SoPhieu"].ToString();

                dlLoaiVatLieu.SelectedValue = dt.Rows[0]["LoaiVL"].ToString();
                dlBenBan.SelectedValue = dt.Rows[0]["BenBan"].ToString();
                txtTLTong.Text = dt.Rows[0]["TLTong"].ToString();

                txtTLBi.Text = dt.Rows[0]["TLBi"].ToString();

                txtTLHang.Text = dt.Rows[0]["TLHang"].ToString();

                
                txtGioNhap.Text = dt.Rows[0]["GioNhap"].ToString();


                txtGhiChu.Text = dt.Rows[0]["GhiChu"].ToString();

                txtLaiXe.Text = dt.Rows[0]["TenLaiXe"].ToString();
                txtDVT.Text = dt.Rows[0]["DonViTinh"].ToString();
                

                dlTramTron.SelectedValue = dt.Rows[0]["TramTronID"].ToString();

                txtDoAm.Text = dt.Rows[0]["DoAm"].ToString();

                
                ckKLMuaVAT.Checked = dt.Rows[0]["KLMuaVAT"].ToString().Trim().Equals("1") ? true : false;
                ckCuocVCVAT.Checked = dt.Rows[0]["CuocVCVAT"].ToString().Trim().Equals("1") ? true : false;
                txtTiLeQuiDoi.Text = dt.Rows[0]["TiLeQuiDoi"].ToString();
                txtKLSauQuiDoi.Text = dt.Rows[0]["KLSauQuiDoi"].ToString();
                txtKLMua.Text = dt.Rows[0]["KLMua"].ToString();
                
                txtBienSo.Text = dt.Rows[0]["BienSo"].ToString();
                txtKLThucTe.Text = dt.Rows[0]["KLThucTe"].ToString();

                
                dlLaiXe.SelectedValue = dt.Rows[0]["LaiXe"].ToString();
                
                hdLaiXeID.Value = dt.Rows[0]["LaiXe"].ToString();

                dlBienSo.SelectedValue = dt.Rows[0]["BienSoID"].ToString();
                
                //dlLaiXe.Enabled = false;
                //dlLoaiVatLieu.Enabled = false;
                //dlBenChayCuoc.Enabled = false;
                //dlBenBan.Enabled = false;
                //dlTramTron.Enabled = false;

                
            }
            btncapnhat.Visible = true;
            btnSave.Visible = false;
            btncapnhat.Visible = CheckKhoa2(txtNgayNhap.Text.Trim(), frmName, Session["user"].ToString());
            if (btncapnhat.Visible == false)
            {
                txtNgayNhap.Enabled = false;
            }
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                if (f_Xoa.Equals(1))
                {
                    if (CheckKhoa(txtNgayNhap.Text.Trim()))
                    {
                        ex.ExecuteNonQuerry("sp_xdanhaphang_Del", p);
                        btnCancel_Click(sender, e);
                        GetMess("Xóa thành công", "");
                    }
                    else
                        GetMess("Dữ liệu đang bị khóa, không thể xóa", "");
                }
                else GetMess("Bạn không có quyền xóa trên trang này", "");

            }
    }
    void UpdateAutoNumber()
    {
        SqlParameter[] p2 = new SqlParameter[4];
        p2[0] = new SqlParameter("@Name", "NhapHang");
        p2[0].SqlDbType = SqlDbType.VarChar;
        p2[1] = new SqlParameter("@Number",int.Parse(LoadSoPhieu()));
        p2[1].SqlDbType = SqlDbType.Int;
        p2[2] = new SqlParameter("@Ngay", ex.GetNgayThang(txtNgayNhap.Text.Trim()));

        p2[2].SqlDbType = SqlDbType.SmallDateTime;
        p2[3] = new SqlParameter("@LoaiVL", ex.GetGuid(dlLoaiVatLieu.SelectedValue));
        p2[3].SqlDbType = SqlDbType.UniqueIdentifier;
        ex.ExecuteNonQuerry("sp_vnttblautonumber_Add", p2);

    }
    protected void txtNgayNhap_TextChanged(object sender, EventArgs e)
    {
        if (hdID.Value.Equals(""))
        {
            LoadSoPhieu();
            btnSave.Visible = CheckKhoa2(txtNgayNhap.Text.Trim(),frmName,Session["user"].ToString());    
            LoadLaiXe(dlLaiXe);
            LoadBienSoXe(dlBienSo);    
        }
        //if(!hdID.Value.Equals(""))
        //    btncapnhat.Visible = CheckKhoa2(txtNgayNhap.Text.Trim(), frmName, Session["user"].ToString());
        
        if (hdID.Value.Equals(""))
            btnSave.Visible = CheckKhoa2(txtNgayNhap.Text.Trim(), frmName, Session["user"].ToString());
        else
            btncapnhat.Visible = CheckKhoa2(txtNgayNhap.Text.Trim(), frmName, Session["user"].ToString());
        
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdanhaphang_List", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
        if (GV.Rows.Count > 0)
        {
            GV.FooterRow.Cells[5].Text = "Tổng cộng";
            GV.FooterRow.Cells[5].HorizontalAlign = HorizontalAlign.Center;
            GV.FooterRow.Cells[8].Text = ConvertDecimal(klmua);
            GV.FooterRow.Cells[9].Text = ConvertDecimal(kltt);
            GV.FooterRow.Cells[11].Text = ConvertDecimal(klquidoi);
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdanhaphang_List", p);
        GV.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;

        if (GV.Rows.Count > 0)
        {
            GV.FooterRow.Cells[5].Text = "Tổng cộng";
            GV.FooterRow.Cells[5].HorizontalAlign = HorizontalAlign.Center;
            GV.FooterRow.Cells[8].Text = ConvertDecimal(klmua);
            GV.FooterRow.Cells[9].Text = ConvertDecimal(kltt);
            GV.FooterRow.Cells[11].Text = ConvertDecimal(klquidoi);
        }

    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdanhaphang_List", p);
        GV.DataBind();

        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;

        if (GV.Rows.Count > 0)
        {
            GV.FooterRow.Cells[5].Text = "Tổng cộng";
            GV.FooterRow.Cells[5].HorizontalAlign = HorizontalAlign.Center;
            GV.FooterRow.Cells[8].Text = ConvertDecimal(klmua);
            GV.FooterRow.Cells[9].Text = ConvertDecimal(kltt);
            GV.FooterRow.Cells[11].Text = ConvertDecimal(klquidoi);
        }
    }
    protected void btnXem_Click(object sender, EventArgs e)
    {
        int index = 6;
        if (dlBenChayCuocLoc.SelectedIndex > 0 && dlNhaCungCapLoc.SelectedIndex.Equals(0) && dlLoaiVLLoc.SelectedIndex.Equals(0))
            index = 1;
        else if (dlBenChayCuocLoc.SelectedIndex > 0 && dlNhaCungCapLoc.SelectedIndex.Equals(0) && dlLoaiVLLoc.SelectedIndex>0)
            index = 2;
        else if (dlBenChayCuocLoc.SelectedIndex > 0 && dlNhaCungCapLoc.SelectedIndex>0 && dlLoaiVLLoc.SelectedIndex>0)
            index = 3;
        else if (dlBenChayCuocLoc.SelectedIndex.Equals(0) && dlNhaCungCapLoc.SelectedIndex>0 && dlLoaiVLLoc.SelectedIndex>0)
            index = 4;
        else if (dlBenChayCuocLoc.SelectedIndex > 0 && dlNhaCungCapLoc.SelectedIndex>0 && dlLoaiVLLoc.SelectedIndex.Equals(0))
            index = 5;
        else if (dlBenChayCuocLoc.SelectedIndex.Equals(0) && dlNhaCungCapLoc.SelectedIndex.Equals(0) && dlLoaiVLLoc.SelectedIndex.Equals(0))
            index = 6;
        else if (dlBenChayCuocLoc.SelectedIndex.Equals(0) && dlNhaCungCapLoc.SelectedIndex.Equals(0) && dlLoaiVLLoc.SelectedIndex>0)
            index = 7;
        else if (dlBenChayCuocLoc.SelectedIndex.Equals(0) && dlNhaCungCapLoc.SelectedIndex>0 && dlLoaiVLLoc.SelectedIndex.Equals(0))
            index = 8;
        
        getDate();

        object tn = ex.GetNgayThang(hdTuNgay.Value);
        object dn = ex.GetNgayThang(hdDenNgay.Value);
        object bcc=ex.GetGuid(dlBenChayCuocLoc.SelectedValue);
        object lvl = ex.GetGuid(dlLoaiVLLoc.SelectedValue);
        object bb = ex.GetGuid(dlNhaCungCapLoc.SelectedValue);

        p = new SqlParameter[6];
        p[0]=new SqlParameter("@TuNgay",tn);
        p[0].SqlDbType=SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", dn);
        p[1].SqlDbType=SqlDbType.SmallDateTime;
        p[2] = new SqlParameter("@BenChayCuoc",bcc );
        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
        p[3] = new SqlParameter("@LoaiVL", lvl);
        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
        p[4] = new SqlParameter("@BenBan", bb);
        p[4].SqlDbType = SqlDbType.UniqueIdentifier;
        p[5] = new SqlParameter("@Index", index);
        p[5].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdanhaphang_Search", p);
        GV.DataBind();

        p = new SqlParameter[6];
        p[0] = new SqlParameter("@TuNgay", tn);
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", dn);
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        p[2] = new SqlParameter("@BenChayCuoc", bcc);
        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
        p[3] = new SqlParameter("@LoaiVL", lvl);
        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
        p[4] = new SqlParameter("@BenBan", bb);
        p[4].SqlDbType = SqlDbType.UniqueIdentifier;
        p[5] = new SqlParameter("@Index", index);
        p[5].SqlDbType = SqlDbType.Int;

        dt = ex.GetData("sp_xdanhaphang_Search_GetFooter", p);
        if (dt.Rows.Count > 0)
        {
            kltt = double.Parse(dt.Rows[0]["TongKLThucTe"].ToString());
            klmua = double.Parse(dt.Rows[0]["TongKLMua"].ToString());
            klquidoi = double.Parse(dt.Rows[0]["TongKL"].ToString());

            GV.FooterRow.Cells[5].Text = "Tổng cộng";
            GV.FooterRow.Cells[5].HorizontalAlign = HorizontalAlign.Center;
            GV.FooterRow.Cells[8].Text = ConvertDecimal(klmua);
            GV.FooterRow.Cells[9].Text = ConvertDecimal(kltt);
            GV.FooterRow.Cells[11].Text = ConvertDecimal(klquidoi);
        }

        btnFirst.Visible = false;
        btnPre.Visible = false;
        btnNext.Visible = false;
    }
    protected void dlThang_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            txtTuNgay.Visible = true;
            txtDenNgay.Visible = true;
            dlNam.Visible = false;
        }
        else
        {
            txtTuNgay.Visible = false;
            txtDenNgay.Visible = false;
            dlNam.Visible = true;
        }
        getDate();
        
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        
        dlNhaCungCapLoc.DataSource = ex.GetData("sp_xdanhaphang_ListBenBanByTime", p);
        dlNhaCungCapLoc.DataBind();
        dlNhaCungCapLoc.Items.Insert(0, new ListItem("--Chọn bên bán--", ""));

        
    }
    public void getDate()
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
        else
        {
            txtTuNgay.Text = "1/" + dlThang.SelectedValue + "/" + dlNam.SelectedValue;
            txtDenNgay.Text = GetLastDayOfMonth(int.Parse(dlThang.SelectedValue)).ToString("dd/MM/yyyy");
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
    }
    public DateTime GetLastDayOfMonth(int iMonth)
    {
        DateTime dtResult = new DateTime(int.Parse(dlNam.SelectedValue), iMonth, 1);
        dtResult = dtResult.AddMonths(1);
        dtResult = dtResult.AddDays(-(dtResult.Day));
        return dtResult;
    }
    protected void dlNhaCungCapLoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        getDate();
        p = new SqlParameter[3];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        p[2] = new SqlParameter("@BenBan", ex.GetGuid(dlNhaCungCapLoc.SelectedValue));
        p[2].SqlDbType = SqlDbType.UniqueIdentifier;

        dlLoaiVLLoc.DataSource = ex.GetData("sp_xdanhaphang_ListLoaiVLByTimeAndNCC", p);
        dlLoaiVLLoc.DataBind();
        dlLoaiVLLoc.Items.Insert(0, new ListItem("--Chọn loại vật liệu--", ""));
        
        ///////////////////////////////////////////////////
        
        p = new SqlParameter[4];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        p[2] = new SqlParameter("@BenBan", ex.GetGuid(dlNhaCungCapLoc.SelectedValue));
        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
        p[3] = new SqlParameter("@LoaiVL", ex.GetGuid(dlLoaiVLLoc.SelectedValue));
        p[3].SqlDbType = SqlDbType.UniqueIdentifier;

        dlBenChayCuocLoc.DataSource = ex.GetData("sp_xdanhaphang_ListBenChayCuocByTime", p);
        dlBenChayCuocLoc.DataBind();
        dlBenChayCuocLoc.Items.Insert(0, new ListItem("--Chọn bên chạy cước--", ""));
    }
    protected void dlBienSo_SelectedIndexChanged(object sender, EventArgs e)
    {
        //
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@ThoiGian", ex.GetNgayThang(txtNgayNhap.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@BienSo", ex.GetGuid(dlBienSo.SelectedValue));
        p[1].SqlDbType = SqlDbType.UniqueIdentifier;

        dt = ex.GetData("sp_xdanhaphang_GetLaiXeByBienSo", p);
        if (dt.Rows.Count > 0)
        {
            txtLaiXe.Text = dt.Rows[0][0].ToString();
            hdLaiXeID.Value = dt.Rows[0][1].ToString();
        }
        else
        {
            txtLaiXe.Text = "";
            hdLaiXeID.Value = "";
        }
    }
    protected void dlBenBan_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (!txtNgayNhap.Text.Trim().Equals("") && dlBenBan.SelectedIndex > 0 && dlLoaiVatLieu.SelectedIndex > 0)
            {
                p = new SqlParameter[3];
                p[0] = new SqlParameter("@BenBan", ex.GetGuid(dlBenBan.SelectedValue));
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@LoaiVL", ex.GetGuid(dlLoaiVatLieu.SelectedValue));
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                p[2] = new SqlParameter("@ThoiGian", ex.GetNgayThang(txtNgayNhap.Text.Trim()));
                p[2].SqlDbType = SqlDbType.SmallDateTime;

                dt = ex.GetData("sp_xdatylequydoi_getNhapHang", p);
                if (dt.Rows.Count > 0)
                {
                    txtDVT.Text = dt.Rows[0]["DonVi"].ToString();
                    txtTiLeQuiDoi.Text = dt.Rows[0]["TyLe"].ToString();
                }
                else
                {
                    txtTiLeQuiDoi.Text = "";
                    txtDVT.Text = "";
                }
            }
            else
            {
                txtTiLeQuiDoi.Text = "";
                txtDVT.Text = "";
            }
            LoadSoPhieu();
        }
        catch
        {
            txtTiLeQuiDoi.Text = "";
            txtDVT.Text = "";
        }
    }
    string ConvertDecimal(double s)
    {
        string q = "";
        if (s.ToString().Contains("."))
        {
            double x = double.Parse(s.ToString().Substring(0, s.ToString().IndexOf('.')));
            q = (x != 0 && x > 999) ? string.Format("{0:0,0}", x) : x.ToString();
            q += s.ToString().Substring(s.ToString().IndexOf('.'));
        }
        else
            q = (s != 0 && s > 999) ? string.Format("{0:0,0}", s) : s.ToString();
        return q;
    }

}